package com.banzhi.shop.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.MarketManaging.Market_Data;
import com.banzhi.shop.entity.MarketManaging.Orderinfo_data;
import com.banzhi.shop.model.CallBack.MarketManagingCallBack;
import com.banzhi.shop.model.IMarketManagingModel;
import com.banzhi.shop.model.Impl.MarketManagingImpl;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.mob.MobSDK.getContext;

/**
 * 销售管理
 */
public class MarketManagingActivity extends BaseActivity implements View.OnClickListener, MarketManagingCallBack, SwipeRefreshLayout.OnRefreshListener {
    /**
     * 退出
     */
    private ImageView ivback;
    /**
     * 销售列表
     */
    private RecyclerView recyclerview;
    private List<Orderinfo_data> orderinfo = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    private IMarketManagingModel model;
    private Market_Data data;
    private CommonHeadAdapter<Orderinfo_data> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_managing);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        model = new MarketManagingImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(this);
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_listview);
        swipeRefreshLayout.setColorSchemeResources(R.color.themeColor);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(layoutManager);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void showViews() {
        adapter = new CommonHeadAdapter<Orderinfo_data>(this, R.layout.item_marketmanaging, R.layout.item_marketmanging_head, orderinfo) {
            @Override
            public void convert(ViewHolder holder, Orderinfo_data orderinfo_data, int position) {
                holder.setText(R.id.tv_money, NumberUtils.DecimalFormat(orderinfo_data.getGoodsprice()));
                holder.setText(R.id.tv_date, DateUtil.getData(orderinfo_data.getPaytime()));
            }

            @Override
            public void headconvert(HeaderViewHolder holder) {
                holder.setText(R.id.tv_allmoney, NumberUtils.DecimalFormat(data.getOrderSum()));
            }
        };
        recyclerview.setAdapter(adapter);

    }

    /**
     * @param object 获取信息成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        data = (Market_Data) object;
        orderinfo.clear();
        orderinfo.addAll(data.getOrderinfo());

        showViews();
        LoadingDialogUtils.dismiss();
    }


    /**
     * @param object 获取信息失败
     */
    @Override
    public void ongetDataError(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        LoadingDialogUtils.dismiss();
    }

    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(this);
    }
}
